package com.daylightrobbery.server.packet.login;

import com.daylightrobbery.entity.Account;
import com.daylightrobbery.server.GameManager;
import com.daylightrobbery.server.packet.type.DataPacket;
import com.daylightrobbery.service.AccountService;
import com.daylightrobbery.utils.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SignUpReqPacket implements DataPacket {

    public int type = 10005;
    public String account;
    public String password;
    public String phone;

    public AccountService accountService = GameManager.Instance.accountService;

    @Override
    public DataPacket process() {
        String ctime = TimeUtils.getCurrentTime();
        SignUpRespPacket resp = new SignUpRespPacket();
        Account accountByAccount = accountService.getAccountByAccount(account);
        if (accountByAccount != null) {
            resp.code = SignUpRespPacket.FAILED;
            resp.msg = "该账号已存在！";
            return resp;
        }
        Account account = new Account();
        account.setAccount(this.account)
                .setPassword(this.password)
                .setCtime(ctime)
                .setPhone(phone);
        int i = accountService.addAccount(account);
        if(i != 0){
            resp.code = SignUpRespPacket.SUCCESS;
        }else{
            resp.code = SignUpRespPacket.FAILED;
            resp.msg = "服务器错误，请稍后重试";
        }
        return resp;
    }

    @Override
    public int getType() {
        return type;
    }
}
